﻿using Microsoft.CSharp;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Experimentor
{
    public partial class form_EditCode : Form
    {
        private UserdefFunction _func;
        
        public form_EditCode()
        {
            InitializeComponent();
        }

        public form_EditCode(UserdefFunction func) : this()
        {
            this._func = func;
            UserdefFunctionType functype = this._func.getFunctiontypeInstance();
            this.label_FunctionHeader.Text = "public static " + functype.Returntype + " " + this._func.Name + "(" + functype.Params + ")\n{";
            this.textBox_Code.Text = func.Code;
        }

        public string getCode()
        {
            return this.textBox_Code.Text;
        }

        private void button_OK_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        private void button_Abbrechen_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Abort;
            this.Close();
        }

        private void button_Test_Click(object sender, EventArgs e)
        {
            string source = @"
namespace Experimentor
{
    public static class User_defined_functions
    {
        " + this.label_FunctionHeader.Text
          + this.textBox_Code.Text
          + this.label_FunctionEnd.Text
          + @"
    }
}
";
            Dictionary<string, string> providerOptions = new Dictionary<string, string>
                {
                    {"CompilerVersion", "v3.5"}
                };
            CSharpCodeProvider provider = new CSharpCodeProvider(providerOptions);
            CompilerParameters compilerParams = new CompilerParameters
            {
                GenerateInMemory = true,
                GenerateExecutable = false
            };
            compilerParams.ReferencedAssemblies.Add(Assembly.GetExecutingAssembly().Location);
            compilerParams.ReferencedAssemblies.Add("System.Windows.Forms.dll");
            compilerParams.ReferencedAssemblies.Add("EPPlus.dll");
            CompilerResults results = provider.CompileAssemblyFromSource(compilerParams, source);
            if (results.Errors.Count == 0) MessageBox.Show(this,"Code can be compiled.","Information",MessageBoxButtons.OK,MessageBoxIcon.Information);
            else
            {
                string errors = "";
                foreach (CompilerError error in results.Errors) errors += ( "\n" + error.Line + ": " + error.ErrorText );
                MessageBox.Show(this,"Errors:\n"+errors,"Warning",MessageBoxButtons.OK,MessageBoxIcon.Warning);
            }
        }
    }
}
